﻿//获取用户信息
var getUserID = function(){
	try{
		debugger;
		var userid = sessionStorage.getItem('userid');
		if(userid!=undefined){
			return userid;
		}else{
			alert('登录信息过期，请重新登录！！');
			window.location.href = '/';
		}
		
	}catch(e){
		alert('登录信息过期，请重新登录！！');
		window.location.href = '/';
	}
}
var userinfo;
getUserInfo(getUserID(),function(result){
	if(result.result){
		userinfo =  result.data[0];
	}else{
		userinfo = {username:'', phone:'',email:'',year:'',month:'',day:'',sex:'男'}
	}
	//生日为空
	if(userinfo.birthday==undefined||userinfo.birthday==""){
		userinfo.birthday = "--";
	}
	//性别为空
	if(userinfo.sex==undefined||userinfo.sex==""){
		userinfo.sex = '男';
	}
var app = angular.module('myLogin', []);
app.controller('myLoginCtrl', function($scope) {
//年列表，最近100年
$scope.years = function(){
	var dt = new Date();
	var maxYear = dt.getFullYear();
	var minYear = dt.getFullYear()-100;
	var ret = [];
	for(var i=maxYear;i>=minYear;i--){
		ret[ret.length] = i;
	}
	return ret;
}();
//月列表 12个月
$scope.months = [1,2,3,4,5,6,7,8,9,10,11,12];
//天列表，根据年月获得
$scope.days = [];
//用户信息

$scope.user = {username:userinfo.username, phone:userinfo.phoneNum,email:userinfo.email,year:parseInt(userinfo.birthday.split('-')[0]),
					month:parseInt(userinfo.birthday.split('-')[1]),day:parseInt(userinfo.birthday.split('-')[2]),sex:userinfo.sex};

//提示信息
$scope.info = "";
//年月变化后日跟着对应
$scope.yearMonthChange = function(){
	if($scope.user.year==''||$scope.user.month==''){
		return;
	}
	var date = new Date($scope.user.year,$scope.user.month,0);
	var daysInMonth = date.getDate();
	var ret = [];
	for(var i=0;i<daysInMonth;i++){
		ret[ret.length] = i+1;
	}
	$scope.days = ret;
};
//手动刷新，否则初始化时不会触发。
$scope.yearMonthChange();
$scope.save = function(){

	//---------------------------16/04/20  樊-------------------------------
	//----------增加了前台页面的js判断，昵称，手机号，邮箱等的输入格式规范--
	//-----------------------------------------------------------------------
	//重置div显示值
	$scope.info = "";
	//判断用户名是否为空                       
	if(/^$/.test($scope.user.username)){
		$scope.info = "请输入昵称号！";
		return false;
	}
	//判断手机是否为空    
	if(isNaN($scope.user.year) || isNaN($scope.user.month) || isNaN($scope.user.day)){
		$scope.info = "请输入完整的生日！";
		return false;
	}
	
	if($scope.user.month>((new Date()).getMonth()+1)||($scope.user.month==((new Date()).getMonth()+1)&&$scope.user.day>(new Date()).getDate())){
		$scope.info = "请输入正确的生日！";
		return false;
	}
	//判断手机是否为空                   
	if(/^$/.test($scope.user.phone)){
		$scope.info = "请输入手机号码！";
		return false;
	}
	//判断手机号码是否数字                       
	if(isNaN($scope.user.phone)){
		$scope.info = "手机号码格式不正确！";
		return false;
	}	
	//判断手机号码是否为11位                   
	if($scope.user.phone.length!=11){
		$scope.info = "请输入正确的手机号码！";
		return false;
	}
	//判断邮箱是否为空               
	if(/^$/.test($scope.user.email)){
		$scope.info = "请输入邮箱！";
		return false;
	}
	if(/^$/.test($scope.user.email)){
		$scope.info = "请输入邮箱！";
		return false;
	}
	reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
    if(!reg.test($scope.user.email))
    {
    	$scope.info = "非法的电子邮件！";
        return false;
    }
	if(/^$/.test($scope.user.year)||/^$/.test($scope.user.month)||/^$/.test($scope.user.day)){
		$scope.info = "请输入完整的生日";
		return false;
	}
	//---------------------------------以上-----------------------------------
	//---------------------------------保存数据-----------------------------------

	saveUserInfo(getUserID(),$scope.user.username,$scope.user.sex,$scope.user.year+'-'+$scope.user.month+'-'+$scope.user.day,$scope.user.phone,$scope.user.email,function(result){
		if(result.result){
			alert("信息修改成功。")
		}else{
			$scope.info = result.info;
		}
	});
};
});
});
